package net.sppan.base.service.impl;
 
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import net.sppan.base.dao.ICommentExuserDao;
import net.sppan.base.dao.support.IBaseDao;
import net.sppan.base.entity.CommentExuser;
import net.sppan.base.entity.CommentExuserView;
import net.sppan.base.entity.ContentExuser;
import net.sppan.base.service.ICommentExuserService;
import net.sppan.base.service.IDyContentCommentService;
import net.sppan.base.service.specification.SimpleSpecificationBuilder;
import net.sppan.base.service.specification.SpecificationOperator.Operator;
import net.sppan.base.service.support.impl.BaseServiceImpl;


@Service
public class CommentExuserServiceImpl extends BaseServiceImpl<CommentExuser, Long> implements ICommentExuserService {

	@Autowired
	private ICommentExuserDao  commentExuserDao;
	@Autowired
	private IDyContentCommentService   contentCommentService;
	
	@Override
	public IBaseDao<CommentExuser, Long> getBaseDao() {
		return this.commentExuserDao;
	}
 
	@Override
	public void saveAll(List<CommentExuser>  list){
		commentExuserDao.saveAll(list);
	}
	
	@Async("distributionAsync")
	@Override
	@Transactional(isolation = Isolation.READ_COMMITTED)
	public synchronized void  distributionBy(String userId,Integer size){ 
		if(size!=null&&size <20){
			SimpleSpecificationBuilder<CommentExuser> builder2 = new SimpleSpecificationBuilder<CommentExuser>();
			builder2.add("exResult", Operator.eq.name(), "0");
			builder2.add("exuserId", Operator.isNull.name(), "");
			Page<CommentExuser> pageContentExusers=commentExuserDao.findAll(builder2.generateSpecification(),new PageRequest(0, 100-size, new Sort(Direction.DESC, "commentId")));
			for(CommentExuser  commentExuser:pageContentExusers.getContent()){
				Optional<CommentExuser> optional =commentExuserDao.findById(commentExuser.getId());
				if(optional!=null&&optional.get().getExResult().equals("0")){
					commentExuserDao.updateCommentExuserDao("0", Long.valueOf(userId), new Date(),commentExuser.getId());
				}
			}
		}
	}
	  
	@Override
	public Page<CommentExuserView> findCommentExuserPage(Long userId, Pageable pageable){
		return commentExuserDao.findCommentExuserPage(userId, pageable);
	}
	@Override
	public void updateCommentExuserBlocked(String publisherDyId){
		SimpleSpecificationBuilder<CommentExuser> builder = new SimpleSpecificationBuilder<CommentExuser>();
		builder.add("commentDyId", Operator.eq.name(), publisherDyId);
		builder.add("exResult", Operator.eq.name(),"0");
		List<CommentExuser> commentExusers=findList(builder.generateSpecification(),new Sort(Direction.DESC,"allotTime"));
		for(CommentExuser commentExuser:commentExusers){
			commentExuser.setExResult("3"); 
			commentExuser.setExTime(new Date()); 
			commentExuser.setExDesc("禁言操作");
			commentExuser.setExuserId(null);
			update(commentExuser);
			contentCommentService.updateContentComment(commentExuser.getCommentId(), 3L);
		}
	}

}
